package com.buptlogistics.creditinvestigation.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.buptlogistics.creditinvestigation.R;
import com.buptlogistics.creditinvestigation.activity.CommonActivity;
import com.buptlogistics.creditinvestigation.activity.LoginRegistActivity;
import com.buptlogistics.creditinvestigation.app.AppData;
import com.buptlogistics.creditinvestigation.app.BaseFragment;
import com.buptlogistics.creditinvestigation.utils.Logcat;


/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {

    private View view;
    private Button btn_loan;
    private Button btn_repay;
    private TextView tv_main_money;
    private TextView tv_main_money_help;

    private String chance;
    private String company_code;

    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_main, container, false);
        AppData.getInstance(MainFragment.this.getActivity());

        initViews();
        initEvents();
        return view;
    }

    protected void initViews() {
        btn_loan = (Button) findViewById(R.id.btn_main_loan);
        btn_repay = (Button) findViewById(R.id.btn_main_repayment);
        tv_main_money = (TextView) findViewById(R.id.tv_main_money);
        tv_main_money_help = (TextView) findViewById(R.id.tv_main_money_help);

    }

    @Override
    public void onResume() {
        if (!AppData.getInstance(MainFragment.this.getActivity()).getIsLogin()) {
            tv_main_money.setText("请登录");
            tv_main_money.setTextSize(40);
            tv_main_money_help.setText("");
        } else {
//            company_code = AppData.getInstance(MainFragment.this.getActivity()).getCompanyCode();
//            /**
//             *获取当前公司评分
//             */
            chance = ""+83;
//            Logcat.i("company_code", company_code);
//            Logcat.i("chance", chance);
            tv_main_money.setText("" + (int) (Float.parseFloat(chance) ));
            tv_main_money_help.setText("当前信用度评分");
        }
        super.onResume();
    }

    protected void initEvents() {
        btn_loan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO
                if (AppData.getInstance(MainFragment.this.getActivity()).getIsLogin()) {
                    Bundle data = new Bundle();
                    data.putInt("fragmentType", 1);
                    // 创建一个Intent
                    Intent intent = new Intent(MainFragment.this.getActivity(),
                            CommonActivity.class);
                    intent.putExtras(data);
                    // 启动intent对应的Activity
                    startActivity(intent);
                } else {
                    startActivity(LoginRegistActivity.class);
                }

            }
        });
        //btn_repay.setOnClickListener(new View.OnClickListener() {
        //    @Override
        //    public void onClick(View view) {
        //        if (AppData.getInstance(MainFragment.this.getActivity()).getIsLogin()) {
        //            Bundle data = new Bundle();
        //            data.putInt("fragmentType", 3);
        //            // 创建一个Intent
        //            Intent intent = new Intent(MainFragment.this.getActivity(),
        //                    CommonActivity.class);
        //            intent.putExtras(data);
        //            // 启动intent对应的Activity
        //            startActivity(intent);
        //        } else {
        //            startActivity(LoginRegistActivity.class);
        //        }
        //    }
        //});
    }

    protected View findViewById(int id) {
        return view.findViewById(id);
    }
}
